package com.jye.rseven.user.serivce.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.jye.rseven.domain.Credit;
import com.jye.rseven.domain.User;
import com.jye.rseven.user.mapper.CreditMapper;
import com.jye.rseven.user.mapper.UserMapper;
import com.jye.rseven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CreditMapper creditMapper;

    private  String WX_AUTH_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";


    @Override
    public String commiteCode(String code,String appId,String appKey) {
        if(StringUtils.isEmpty(code)){
            return "参数为空";
        }
        RestTemplate restTemplate = new RestTemplate();
        StringBuffer loginUrl = new StringBuffer();
        loginUrl.append(WX_AUTH_LOGIN_URL).append("?appid=")
                .append(appId).append("&secret=")
                .append(appKey).append("&js_code=").append(code)
                .append("&grant_type=authorization_code");
        String msg = restTemplate.getForObject(loginUrl.toString(),String.class);
        return msg;
    }

    @Override
    public User findUserByOpenId(String openId) {
        User t_user = userMapper.findUserByOpenId(openId);
        if(t_user==null){
            t_user = new User(openId,"暂无昵称",
                    "http://39.106.100.179:9999/group1/M00/00/00/J2pks16oTJ2AClMsAAHw8xgOVfY843.jpg");
            userMapper.addUser(t_user);
            Credit credit = new Credit();
            credit.setUId(t_user.getId());
            credit.setCredit(0);
            creditMapper.addCredit(credit);
            t_user = userMapper.findUserByOpenId(openId);
        }
        return t_user;
    }

    @Override
    public User findUserById(int id) {
        User user = userMapper.findUserById(id);
        user.setCredit(creditMapper.findMyCreditByUserId(id));
        return user;
    }

    @Override
    public void userLoginById(int id) {
        Date date = new Date();
        User user = new User();
        user.setId(id);
        user.setLastLoginTime(date);
        userMapper.updateUser(user);
    }

    @Override
    public int updateUser(User user) {
        try{
            userMapper.updateUser(user);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<Date> findUserLoginHistoryById(int id) {
        List<Date> dateList = userMapper.findUserLoginHistoryById(id);
        return dateList;
    }

    @Override
    public int addUser(User user) {
        try{
            int i = userMapper.addUser(user);
            return i;
        }catch (Exception e){
            return 0;
        }
    }
}
